<template>
  <div>
    <el-card style="min-width: 1200px">
      <div class="tableSelectWrap">
        <el-form ref="commandForm" :model="commandForm" label-width="100px">
          <el-row>
            <el-col :span="7">
              <el-form-item label="选择时间" prop="date">
                <el-date-picker
                  :default-time="['00:00:00', '23:59:59']"
                  style="width: 100%"
                  v-model="commandForm.date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetimerange"
                  :clearable="false"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="支付渠道" prop="chargeChannel">
                <el-select
                  v-model="commandForm.chargeChannel"
                  class="leftSelect"
                  style="width: 100%"
                  placeholder="请选择支付渠道"
                  clearable
                  @change="handleChannel"
                >
                  <el-option
                    v-for="(item, index) of payChannel"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="支付状态" prop="status">
                <el-select
                  v-model="commandForm.status"
                  style="width: 100%"
                  placeholder="请选择支付状态"
                  clearable
                >
                  <el-option
                    v-for="(item, index) of payStateList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="7">
              <el-form-item label="退款情况" prop="refundState">
                <el-select
                  v-model="commandForm.refundState"
                  style="width: 100%"
                  placeholder="请选择退款情况"
                  clearable
                >
                  <el-option
                    v-for="(item, index) of refundCaseList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1" style="min-width: 170px">
              <el-button class="width120" @click="this.resetFields"
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="
                  () => {
                    this.dataForm = JSON.parse(
                      JSON.stringify(this.commandForm)
                    );
                    this.handleQuery();
                  }
                "
                >查询</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="textBox"
      style="min-width: 1200px; margin-bottom: -5px; margin-top: 15px"
    >
      <span class="numBox">
        <span>订单金额</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtOrderTotalAmount || 0.0 }}</span>
      </span>
      <span class="numBox">
        <span>已支付</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtPayTotalAmount || 0.0 }}</span>
      </span>
      <span class="numBox">
        <span>已赠送</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtGiveTotalAmount || 0.0 }}</span>
      </span>
      <span class="numBox">
        <span>用户手续费</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtFeeTotalAmount || 0.0 }}</span>
      </span>
    </el-card>
    <el-card style="margin-top: 20px; min-width: 1200px">
      <div class="switch">
        <el-switch v-model="isFixedTableH" inactive-text="固定表头">
        </el-switch>
      </div>
      <el-table :data="dataList" :height="fixedTableHeight">
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="innerRechargeNo"
          align="center"
          label="商户订单号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.innerRechargeNo ? scope.row.innerRechargeNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fromUserNo"
          align="center"
          label="充值用户"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fromUserNo ? scope.row.fromUserNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtAmount"
          align="center"
          label="充值金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtAmount ? scope.row.fmtAmount : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtBalanceBonus"
          align="center"
          label="赠送金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.fmtBalanceBonus ? scope.row.fmtBalanceBonus : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="feeAmount"
          align="center"
          label="用户手续费/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.feeAmount ? scope.row.feeAmount : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="chargeChannelName"
          align="center"
          label="支付渠道"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.chargeChannelName ? scope.row.chargeChannelName : "-"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="payStatusName"
          align="center"
          label="支付状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.statusName ? scope.row.statusName : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="refundStateName"
          align="center"
          label="退款情况"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.refundStateName ? scope.row.refundStateName : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="act"
          align="center"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="handleDetails(scope.row)"
              >详情</el-button
            > -->
            <!-- <el-button
              type="text"
              v-if="scope.row.cashierUrl && scope.row.payStatusName == '待支付'"
              @click="goPayFn(scope.row.cashierUrl)"
              >支付</el-button
            > -->
            <el-button type="text" @click="goRefundFn(scope.row)"
              >退款</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
    <el-dialog
      title=""
      class="tkDialog"
      :visible="refundDialog"
      width="449px"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialogTitle">退款申请</div>
      <el-form :model="form" ref="refundForm" label-position="top">
        <el-form-item label="商户订单号" prop="innerRechargeNo">
          <el-input v-model="form.innerRechargeNo" disabled></el-input>
        </el-form-item>
        <el-form-item :label="'退款金额'" prop="refundAmount">
          <el-input
            type="number"
            v-model="form.refundAmount"
            placeholder="输入退款金额"
            disabled
            :step="0.01"
            oninput="if(isNaN(value)) { value = parseFloat(value) } if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+3)}"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="退款理由" prop="refundRemark">
          <el-input
            v-model="form.refundRemark"
            maxlength="30"
            placeholder="输入退款理由"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            refundDialog = false;
            $refs['refundForm'].resetFields();
          "
          class="width120"
          >取消</el-button
        >
        <el-button type="primary" class="width120" @click="confirmRefund"
          >确定</el-button
        >
      </div>
    </el-dialog>
    <!-- 详情抽屉 -->
    <detail-drawer
      :drawer.sync="drawer"
      :info="detailsInfo"
      :activeKey="activeKey"
    ></detail-drawer>
  </div>
</template>

<script>
import detailDrawer from "./detailDrawer.vue";
import mixin from "@/mixins/mixin";
export default {
  name: "rechargeRecord",
  mixins: [mixin],
  components: {
    detailDrawer,
  },
  data() {
    return {
      payformId: null,
      refundRow: {},
      form: {
        innerRechargeNo: "",
        refundRemark: "",
        refundAmount: "",
      },
      refundDialog: false,
      activeKey: "1",
      buttonRole: false,
      drawer: false,
      detailsInfo: {},
      mixinViewModuleOptions: {
        getDataListURL: "/api/merchant/balanceCharge/searchByPage",
        createdIsNeed: false,
      },
      payStateList: [
        // 支付状态
        { id: 1, label: "所有", value: "" },
        { id: 3, label: "待支付", value: "waiting" },
        { id: 2, label: "支付中", value: "pending" },
        { id: 4, label: "充值完成", value: "success" },
        { id: 5, label: "充值失败", value: "fail" },
      ],
      payChannel: [],
      payChannelType: this.$store.state.channelListType,
      payChannelTypeList: [],
      refundCaseList: [
        // 退款情况
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "无退款", value: "no_refund" },
        { id: 3, label: "部分退款", value: "part_refund" },
        { id: 4, label: "全额退款", value: "all_refund" },
      ],
      info: {},
      commandForm: {
        status: "",
        refundState: "",
        chargeChannel: "",
        date: [
          this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
          this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
        ],
        gmtCreatedBegin: this.$moment()
          .startOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        gmtCreatedEnd: this.$moment()
          .endOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
      },
    };
  },
  created() {
    if (this.$store.state.buttonList) {
      this.$store.state.buttonList.forEach((e) => {
        if (e.type == "refund") {
          this.buttonRole = true;
        }
      });
    }
    if (!this.commandForm.chargeChannel) {
      this.payChannelTypeList = [{ label: "所有", value: "" }];
    }

    this.dataForm = JSON.parse(JSON.stringify(this.commandForm));
    this.handleQuery();
    this.getChannelList();
  },
  watch: {
    "$store.state.testLine": {
      handler() {
        if (this.$route.name == this.$options.name) {
          if (this.commandForm.date && this.commandForm.date.length) {
            this.commandForm.gmtCreatedBegin = this.commandForm.date[0];
            this.commandForm.gmtCreatedEnd = this.commandForm.date[1];
          } else {
            this.commandForm.gmtCreatedBegin = null;
            this.commandForm.gmtCreatedEnd = null;
          }
          this.getDataList();
        }
      },
    },
    deep: true,
  },
  methods: {
    getChannelList() {
      this.$http.get("/api/dict/channelList").then((res) => {
        if (res.code === "0000") {
          var a = [{ label: "所有", value: "" }];
          var b = a.concat(res.data);
          this.payChannel = b;
        }
      });
    },
    //重置操作
    resetFields() {
      this.resetForm("commandForm");
    },
    goPayFn(url) {
      window.open(url);
    },
    goRefundFn(row) {
      let that = this;
      that
        .$confirm(
          "确认退款后，无需审核的将直接发起退款，需要审核的需审核人同意后方可发起退款，审核人拒绝则该笔订单状态不变，退款成功后退款金额将于7个工作日内原路原回。",
          "确认执行",
          {
            customClass: "confirm",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            showClose: false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
            type: "warning",
            center: true,
          }
        )
        .then(() => {
          this.payformId = row.id;
          this.refundRow = row;
          this.refundDialog = true;
          this.form.innerRechargeNo = this.refundRow.innerRechargeNo;
          this.form.refundAmount = this.refundRow.fmtActualAmount;
        });
    },
    confirmRefund() {
      this.$refs.refundForm.validate((valid) => {
        if (valid) {
          // 单笔退款
          this.$http
            .post("/api/merchant/refundAudit/add", {
              payformId: this.payformId,
              refundRemark: this.form.refundRemark,
              refundAmt: this.form.refundAmount,
            })
            .then((res) => {
              if (res.code === "0000") {
                this.$message.success("已提交！");
                this.$refs["refundForm"].resetFields();
                this.refundDialog = false;
                this.form.refundRemark = "";
                this.handleQuery();
              }
            });
        }
      });
    },
    handleDetails(row) {
      switch (this.activeKey) {
        case "1":
          this.$http
            .get(`/api/merchant/balanceCharge/get?id=${row.id}`)
            .then((res) => {
              if (res.code === "0000") {
                this.detailsInfo = res.data;
                this.drawer = true;
              }
            });
          break;
        default:
          break;
      }
    },
    getStatisticsInfo() {
      this.$http
        .post("/api/merchant/balanceCharge/chargeStatistics", this.dataForm)
        .then((res) => {
          if (res.code === "0000") {
            this.info = res.data || {};
          }
        });
    },
    handleChannel(val) {
      if (val) {
        this.payChannelTypeList = this.payChannelType[val];
      } else {
        this.payChannelTypeList = [];
      }
    },
    handleQuery() {
      if (this.dataForm.date && this.dataForm.date.length) {
        this.dataForm.gmtCreatedBegin = this.dataForm.date[0];
        this.dataForm.gmtCreatedEnd = this.dataForm.date[1];
      } else {
        this.dataForm.gmtCreatedBegin = null;
        this.dataForm.gmtCreatedEnd = null;
      }
      this.getDataList();
      this.getStatisticsInfo();
    },
  },
};
</script>

<style scoped lang="scss">
.switch {
  margin-bottom: 20px;
}
.ml {
  margin-left: 50px;
}
.search {
  position: absolute;
  right: 0;
  top: -164px;
}
</style>
